package org.example.actuator;
import org.example.Interface.ddl.Create;
import org.example.actuator.ibuilder.CreateBuilder;
import org.example.builder.DdlBuilder;
import org.example.model.Field;

import java.util.List;

public abstract class ExecuteCreate<T extends CreateBuilder<T>> extends  DdlBuilder<T>  implements Create {


    public  String createTable(String tableName, List<Field> fieldList) {
        current.create()
                .tableName(tableName)
                .LeftParenthesis()
                .wrap();

        int maxFieldStringLength = 0;
        int maxFieldSqlTypeStringLength = 0;
        for (Field field : fieldList) {
            if (maxFieldStringLength <= field.getTableColumn().length()) {
                maxFieldStringLength = field.getTableColumn().length();
            }
            if (maxFieldSqlTypeStringLength <= field.getSqlType().length()) {
                maxFieldSqlTypeStringLength = field.getSqlType().length();
            }
        }
        maxFieldStringLength++;
        maxFieldSqlTypeStringLength++;

        for (Field field : fieldList) {
            String tableColumn = field.getTableColumn();
            current = current.space(4)
                    .addColumn(String.format("%-" + maxFieldStringLength + "s", tableColumn))
                    .addType(String.format("%-" + maxFieldSqlTypeStringLength + "s", field.getSqlType()))
                    .isPrimaryKey(field.isPrimaryKey());
            if (null != field.getComment()) {
                current.space().addComment(field.getComment());
            }
            current.addComma()
                    .wrap();
        }

        current = current.remove(2)
                .wrap()
                .rightParenthesis();
        return current.end();
    }


    public  String createDataBase(String database){
       return  current.create().dataBase(database).end();
    }


}
